#include "_SetApppageCallback.h"
#include <QString>
#include "AnGlobal.h"
#include "AnAdbServer.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <iostream>
#include <QWidget>
#include <QFileDialog>
#include <QMessageBox>

_SetApppageCallback::_SetApppageCallback()
{

}

_SetApppageCallback::_SetApppageCallback(AnMainwin *anMainwin)
{
    amw=anMainwin;
    movie=new QMovie(":/img/loading.gif");
    connect(amw->ap->app_list,SIGNAL(itemClicked(QTreeWidgetItem *,int)),amw->ap->app_list,SLOT(setCurrentItem(QTreeWidgetItem *,int)));
}

_SetApppageCallback::~_SetApppageCallback()
{

}

void _SetApppageCallback::response(QJsonObject an_resp)
{
    std::cout << "response: \n" << QJsonDocument(an_resp).toJson().data() << std::endl;
    if(!QString::compare(an_resp.value(NAME).toString(),"app_list",Qt::CaseSensitive))
    {
        if(an_resp.value(DATA).toObject().value(RESULT).toInt())
        {
            QJsonArray userApp=an_resp.value(DATA).toObject().value(USER).toArray();
            QJsonArray sysApp=an_resp.value(DATA).toObject().value(SYSTEM).toArray();
            int userAppSize=userApp.size();
            int sysAppSize=sysApp.size();
            userAppList=new QTreeWidgetItem(amw->ap->app_list);
            userAppList->setText(0,"用户应用程序");
            userAppList->setExpanded(true);
            sysAppList=new QTreeWidgetItem(amw->ap->app_list);
            sysAppList->setText(0,"系统应用程序");
            sysAppList->setExpanded(true);
            for(int i=0;i<userAppSize;i++)
            {
                QTreeWidgetItem *userAppItem=new QTreeWidgetItem(userAppList);
                userAppItem->setText(0,userApp[i].toObject().value(APP_NAME).toString());
                QTreeWidgetItem *userPackNameItem=new QTreeWidgetItem(userAppItem);
                userPackNameItem->setText(0,userApp[i].toObject().value(PACK_NAME).toString());
                QTreeWidgetItem *userInsDateItem=new QTreeWidgetItem(userAppItem);
                userInsDateItem->setText(0,"安装时间: "+userApp[i].toObject().value(INS_D).toString());
                QTreeWidgetItem *userVerItem=new QTreeWidgetItem(userAppItem);
                userVerItem->setText(0,"版本: "+userApp[1].toObject().value(VER).toString());
                QTreeWidgetItem *userTypeItem=new QTreeWidgetItem(userAppItem);
                userTypeItem->setText(0,"类型: User");
            }
            if(amw->ap->checkBox_show_sysapp->checkState()==Qt::Checked)
            {
                for(int i=0;i<sysAppSize;i++)
                {
                    QTreeWidgetItem *sysAppItem=new QTreeWidgetItem(sysAppList);
                    sysAppItem->setText(0,sysApp[i].toObject().value(APP_NAME).toString());
                    QTreeWidgetItem *sysPackNameItem=new QTreeWidgetItem(sysAppItem);
                    sysPackNameItem->setText(0,sysApp[i].toObject().value(PACK_NAME).toString());
                    QTreeWidgetItem *sysInsDateItem=new QTreeWidgetItem(sysAppItem);
                    sysInsDateItem->setText(0,"安装日期: "+sysApp[i].toObject().value(INS_D).toString());
                    QTreeWidgetItem *sysVerItem=new QTreeWidgetItem(sysAppItem);
                    sysVerItem->setText(0,"版本: "+sysApp[1].toObject().value(VER).toString());
                    QTreeWidgetItem *sysTypeItem=new QTreeWidgetItem(sysAppItem);
                    sysTypeItem->setText(0,"类型： System");
                }
            }
        }
    }
    if(!QString::compare(an_resp.value(NAME).toString(),"app_install",Qt::CaseSensitive))
    {
        if(an_resp.value(DATA).toObject().value(RESULT).toInt())
        {
            firstDialog->close();
            delete firstDialog;
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIconPixmap(QPixmap(":/img/sign-check.png"));
            firstMessageBox->setText("安装成功");
            firstMessageBox->show();
            amw->ap->app_list->clear();
            QJsonObject reqs;
            reqs.insert(NAME, QJsonValue("app_list"));
            reqs.insert(STATUS, QJsonValue(REQUEST));
            adb_server->adbServerManager(reqs, this);
            std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
        }
        else
        {
            firstDialog->close();
            delete firstDialog;
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIcon(QMessageBox::Icon::Critical);
            firstMessageBox->setText("安装失败");
            firstMessageBox->show();
        }
    }
    if(!QString::compare(an_resp.value(NAME).toString(),"app_uninstall",Qt::CaseSensitive))
    {
        if(an_resp.value(DATA).toObject().value(RESULT).toInt())
        {
            firstDialog->close();
            delete firstDialog;
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIconPixmap(QPixmap(":/img/sign-check.png"));
            firstMessageBox->setText("卸载成功");
            firstMessageBox->show();
            amw->ap->app_list->clear();
            if(AnGlobal::is_connecting())
            {
                QJsonObject reqs;
                reqs.insert(NAME, QJsonValue("app_list"));
                reqs.insert(STATUS, QJsonValue(REQUEST));
                adb_server->adbServerManager(reqs, this);
                std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
            }
        }
        else
        {
            firstDialog->close();
            delete firstDialog;
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIcon(QMessageBox::Icon::Critical);
            firstMessageBox->setText("卸载失败");
            firstMessageBox->show();
        }
    }
}

void _SetApppageCallback::connect_build()
{
    QJsonObject reqs;
    reqs.insert(NAME, QJsonValue("app_list"));
    reqs.insert(STATUS, QJsonValue(REQUEST));
    adb_server->adbServerManager(reqs, this);
    std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
}

void _SetApppageCallback::connect_close()
{
    amw->ap->app_list->clear();
}

void _SetApppageCallback::refresh_clicked()
{
    amw->ap->app_list->clear();
    if(AnGlobal::is_connecting())
    {
        QJsonObject reqs;
        reqs.insert(NAME, QJsonValue("app_list"));
        reqs.insert(STATUS, QJsonValue(REQUEST));

        adb_server->adbServerManager(reqs, this);
        std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
    }
}

void _SetApppageCallback::app_install_clicked()
{
    if(AnGlobal::is_connecting())
    {
       QString filePath=QFileDialog::getOpenFileName(amw, tr("安装应用"), "/home", tr("*.apk"));
       if(filePath==NULL)
       {
           return;
       }

       QJsonObject reqs;
       reqs.insert(NAME,QJsonValue("app_install"));
       reqs.insert(STATUS,QJsonValue(REQUEST));
       QJsonObject data;
       data.insert(PATH,QJsonValue(filePath));
       reqs.insert(DATA,data);
       adb_server->adbServerManager(reqs,this);
       std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
       firstDialog=new QDialog(amw);
       firstDialog->resize(300,100);
       firstDialog->setWindowFlags(Qt::FramelessWindowHint);
       firstDialog->setModal(true);
       firstDialog->move((amw->width()-firstDialog->width())/2,(amw->height()-firstDialog->height())/2);
       QLabel *movLabel=new QLabel(firstDialog);
       QLabel *diaLabel=new QLabel(firstDialog);
       diaLabel->setText("正在安装，请稍候...");
       diaLabel->setGeometry(100,25,150,50);
       movLabel->setMovie(movie);
       movLabel->setGeometry(50,25,50,50);
       firstDialog->open();
       movie->start();
    }
}

void _SetApppageCallback::app_uninstall_clicked()
{
    if(AnGlobal::is_connecting())
    {
        QTreeWidgetItem *currentItem=amw->ap->app_list->currentItem();
        if(currentItem)
        {
            if(currentItem->parent()==userAppList)
            {
                if(!QString::compare(currentItem->text(0),"AnManager",Qt::CaseSensitive))
                {
                    firstMessageBox=new QMessageBox(amw);
                    firstMessageBox->setIcon(QMessageBox::Icon::Warning);
                    firstMessageBox->setText("不可卸载AnManager");
                    firstMessageBox->show();
                    return;
                }
                QJsonObject reqs;
                reqs.insert(NAME,QJsonValue("app_uninstall"));
                reqs.insert(STATUS,QJsonValue(REQUEST));
                QJsonObject data;
                data.insert(PACK_NAME,QJsonValue(currentItem->child(0)->text(0)));
                reqs.insert(DATA,data);
                adb_server->adbServerManager(reqs,this);
                std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
                firstDialog=new QDialog(amw);
                firstDialog->resize(300,100);
                firstDialog->setWindowFlags(Qt::FramelessWindowHint);
                firstDialog->setModal(true);
                firstDialog->move((amw->width()-firstDialog->width())/2,(amw->height()-firstDialog->height())/2);
                QLabel *movLabel=new QLabel(firstDialog);
                QLabel *diaLabel=new QLabel(firstDialog);
                diaLabel->setText("正在卸载，请稍候...");
                diaLabel->setGeometry(100,25,150,50);
                movLabel->setMovie(movie);
                movLabel->setGeometry(50,25,50,50);
                firstDialog->open();
                movie->start();
                return;
            }
            if(currentItem->parent()==sysAppList)
            {
                firstMessageBox=new QMessageBox(amw);
                firstMessageBox->setIcon(QMessageBox::Icon::Warning);
                firstMessageBox->setText("不可卸载系统应用");
                firstMessageBox->show();
            }
        }
    }
}
